package com.aspire.boc.mms.wmf.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aspire.boc.mms.wmf.constants.GlobalConstant;
import com.aspire.boc.mms.wmf.model.SecurityUser;
import com.aspire.boc.mms.wmf.model.WmfAction;
import com.aspire.boc.mms.wmf.model.WmfRoleAuth;
import com.aspire.boc.mms.wmf.permission.PermissionCacheService;
import com.aspire.boc.mms.wmf.service.PermissionService;
@Controller
public class RoleActionAct extends BaseAct {
	
	private static Log log = LogFactory.getLog(RoleActionAct.class);
	
	/**
	 * 
	 * @Title: loadActions 
	 * @Description: TODO(加载工具按钮。工具条部位) 
	 * @param resourceid
	 * @param roleId
	 * @param ownertype
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/action/loadActions")
	public @ResponseBody List<WmfAction> loadActions(
			@RequestParam(value = "resourceid", required = false) String resourceid,
			@RequestParam(value = "selectType", required = false) String selectType,
			@RequestParam(value = "roleId", required = false) String roleId,
			@RequestParam(value = "ownertype", required = false) String ownertype,
			HttpServletRequest request,
			HttpServletResponse response){
		log.info(">>>>>>  access /action/loadActions,parm:(resourceid=" + resourceid + ")");
		List<WmfAction> actions = new ArrayList<WmfAction>();
		if(GlobalConstant.ALL_ACTION_RESOURCE.equals(resourceid) || GlobalConstant.ALL_ACTION_OWNERTYPE.equals(ownertype)){
			actions = permissionCacheService.getActionsResourceId(
					Integer.parseInt(GlobalConstant.ALL_ACTION_RESOURCE),GlobalConstant.ALL_ACTION_OWNERTYPE);
		}else if(resourceid != null){
			List<WmfAction> _actions = permissionCacheService.getActionsResourceId(new Integer(resourceid),ownertype);
			//超级管理员的话不做限制，具有所有的功能
			SecurityUser securityUser = (SecurityUser) request.getSession().getAttribute("securityUser");
			boolean isAdmin = securityUser.isSystemAdmin();
			if(isAdmin){
				for(WmfAction wmfAction : _actions){
					actions.add(wmfAction);
				}
			}else if(ownertype!=null&&"2".equals(ownertype)){
				List<String> roleIds = (List<String>) request.getSession().getAttribute(GlobalConstant.ADMIN_ROLEIDS);
				List<Integer> acitonsIntegers = permissionService.getActionsByRole(roleIds);
				for(WmfAction wmfAction : _actions){
					if(acitonsIntegers.contains(wmfAction.getId())){
						actions.add(wmfAction);
					}
				}
			}else{
				for(WmfAction wmfAction : _actions){
					actions.add(wmfAction);
				}
			}
			
		}
		return actions;
	}
	
	/**
	 * 
	 * @Title: loadResourceTreeActions 
	 * @Description: TODO(加载资源树下功能按钮列表，2以内按钮即可) 
	 * @param resourceid
	 * @param roleId
	 * @param ownertype
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/action/loadResourceTreeActions")
	public @ResponseBody Map<String, List<WmfAction>> loadResourceTreeActions(
			@RequestParam(value = "clickResourceId", required = false) String resourceid,
			@RequestParam(value = "roleId", required = false) String roleId,
			@RequestParam(value = "ownertype", required = false) String ownertype,
			HttpServletRequest request,
			HttpServletResponse response){
		log.info(">>>>>>  access /action/loadActions,parm: (resourceid=" + resourceid + ")");
		//获取选中资源
		Map<String, List<WmfAction>> roleAuths = new HashMap<String, List<WmfAction>>();
		List<WmfAction> actions = new ArrayList<WmfAction>();
		if(resourceid != null){
			actions = permissionCacheService.getActionsResourceId(new Integer(resourceid),"1,2");
		}
		
		List<WmfRoleAuth> wmfRoleAuths = permissionService.getActionAuth(actions);
		for(WmfAction wmfAction : actions){
			wmfAction.setChecked(false);
			for(WmfRoleAuth wmfRoleAuth :wmfRoleAuths){
				if(wmfAction.getId().equals(wmfRoleAuth.getActionId())&&roleId.equals(wmfRoleAuth.getRoleId())){
					wmfAction.setChecked(true);
				}
			}
		}
		roleAuths.put("roleAuths", actions);
		return roleAuths;
	}
	
	/**
	 * 保存角色权限
	 * @param actionId
	 * @param roleId
	 * @param ownertype
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/roleAuth/saveAuth")
	public @ResponseBody Map<String,Object> saveAuth(
			@RequestParam(value = "auth", required = true) String auth,
			@RequestParam(value = "clickResourceId", required = true) String clickResourceId,
			@RequestParam(value = "roleId", required = true) String roleId,
			HttpServletRequest request,
			HttpServletResponse response) {
		log.info(">>>>>>  access /action/saveAuth,parm:(auth=" + auth + "，roleId= " + roleId + ")");
		Map<String,Object> result = new HashMap<String,Object>();
		String resourceid = "";
		try {
			Map<String, Object> authMap = new HashMap<String, Object>();
			if(null != auth&&!"".equals(auth)){
				List<String> authsList = new ArrayList<String>();
				for(String authValue : auth.split(",")){
					authsList.add(authValue.split(":")[1]);
				}
				authMap.put(auth.split(",")[0].split(":")[0], authsList);
			}
			resourceid = auth.split(",")[0].split(":")[0];
			if(null==resourceid||"".equals(resourceid)){
				resourceid = clickResourceId;
			}
			permissionService.updateRoleAuth(authMap,roleId,resourceid);
			result.put("success", Boolean.valueOf(true));
		} catch (Exception e) {
			e.printStackTrace();
			result.put("failure", Boolean.valueOf(true));
		}
		return result;
	}
	
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private PermissionCacheService permissionCacheService;
}
